package com.hna.face.controller.sys;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.hna.face.base.BaseController;
import com.hna.face.common.result.BaseResult;
import com.hna.face.model.BfDict;
import com.hna.face.model.BfUser;
import com.hna.face.service.IBfDictService;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 字典管理
 * 
 * @author zhulei
 * 
 */
@Controller
public class DictController extends BaseController{
	
	@Autowired
	private IBfDictService dictService;
	
	private final Logger logger = LoggerFactory.getLogger(DictController.class);
	
	@RequestMapping("/dict/index")
	public ModelAndView index(HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView("pages/dict/index");
		modelAndView.addObject("typeId", request.getParameter("typeId"));
		
		return modelAndView;
	}
	
	@RequestMapping("/dict/add")
	public ModelAndView add(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("typeId", request.getParameter("typeId"));
		mav.setViewName("pages/dict/add");
		return mav;
	}	
	
	@RequestMapping("/dict/edit")
	public ModelAndView edit(HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView("pages/dict/edit");
		modelAndView.addObject("typeId", request.getParameter("typeId"));
		modelAndView.addObject("id", request.getParameter("id"));

		return modelAndView;
	}
	
	
	/**
	 * 字典列表
	 * 
	 * @param
	 * @return
	 * @throws
	 */
	@RequestMapping(value = { "/rest/dictList" }, method = RequestMethod.GET)
	public @ResponseBody
	BaseResult dictList(HttpServletRequest request,BfDict dict) {
    	try{
    		Wrapper<BfDict> wrapper=new EntityWrapper<BfDict>();
    		wrapper.eq("type_id", dict.getTypeId());
    		
        	if(StringUtils.isNotEmpty( dict.getDictName())){
        		wrapper.like("dict_name", dict.getDictName());
        	}
        	
        	if(StringUtils.isNotEmpty( dict.getDictValue())){
        		wrapper.like("dict_value", dict.getDictValue());
        	}
       	
//    		Page<User> page=new Page<User>(userRequest.getPage(), userRequest.getPageSize());
    		List<BfDict> dictList = dictService.selectList(wrapper)	;
//    		userList.getRecords();
//    		userList.getTotal();
    		Map res = new HashMap();
    		res.put("rows", dictList);
			res.put("total", dictList.size());				
			return BaseResult.success(res);
    		
    	}
    	catch(Exception e){
    		logger.error("error:",e);
    		return BaseResult.exception(e.getMessage());
    	}
	}
	
	
	/**
	 * 添加字典
	 * @param
	 * @return
	 * @throws
	 */
	@RequestMapping(value = { "/rest/dictAdd" }, method = RequestMethod.POST)
	public @ResponseBody
	BaseResult dictAdd(HttpServletRequest request,BfDict dict ) {

		try {
			BfUser user=(BfUser)request.getSession().getAttribute("userInfo");
			dictService.insert(dict);
			this.saveUserlog(request, "字典管理："+user.getUsername()+"添加操作");
			return BaseResult.success();
		} catch (Exception e) {
			logger.error("error:",e);
			return BaseResult.exception(e.getMessage());
		}

	}
	
	/**
	 * 字典详情
	 * 
	 * @param
	 * @return
	 * @throws
	 */
	@RequestMapping(value = { "/rest/dictInfo" }, method = RequestMethod.GET)
	public @ResponseBody
	BaseResult dictInfo(HttpServletRequest request,Long id) {

		try {
			BfDict dict = dictService.selectById(id);
			// res.put("success", "true");
			return BaseResult.success(dict);
		} catch (Exception e) {			
			logger.error("error:",e);
			return BaseResult.exception(e.getMessage());
		}

	}
	
	
	/**
	 * 更新字典值
	 * 
	 * @param
	 * @return
	 * @throws
	 */
	@RequestMapping(value = { "/rest/dictUpdate" }, method = RequestMethod.POST)
	public @ResponseBody
	BaseResult dictUpdate(HttpServletRequest request,BfDict dict ) {

		Map res = new HashMap();
		try {
			BfUser user=(BfUser)request.getSession().getAttribute("userInfo");
			dictService.updateById(dict);
			this.saveUserlog(request, "字典管理："+user.getUsername()+"修改操作");
			return BaseResult.success();
		} catch (Exception e) {
			logger.error("error:",e);
			return BaseResult.exception(e.getMessage());
		}

	}
	
	
	/**
	 * restful
	 * 删除字典
	 * @param req
	 * @return
	 */
	@RequestMapping(value = { "/rest/dictDel" }, method = RequestMethod.POST)
	public @ResponseBody
	BaseResult dictDel(HttpServletRequest request,String ids) {
		try {
			BfUser user=(BfUser)request.getSession().getAttribute("userInfo");
//			String[] ids = request.getParameter("ids").split(",");
			dictService.deleteBatchIds(Arrays.asList(ids.split(",")));
//			dictService.deleteByPrimaryKeys(ids);
            
			this.saveUserlog(request, "字典管理："+user.getUsername()+"删除操作");
			return BaseResult.success();
		} catch (Exception e) {
			logger.error("error:",e);
			return BaseResult.exception(e.getMessage());
		}
	}
	
	
	@RequestMapping(value = { "/rest/api/searchDictList" }, method = RequestMethod.GET)
	public @ResponseBody
	BaseResult searchDictList(HttpServletRequest request,Long typeId)  {
		try {
			Wrapper<BfDict> wrapper=new EntityWrapper<BfDict>();
       	
//    		Page<User> page=new Page<User>(userRequest.getPage(), userRequest.getPageSize());
    		List<BfDict> dictList = dictService.selectList(wrapper)	;
			if(dictList == null || dictList.size()==0)
			{
				return BaseResult.fail("0002", "未查询到值");
			}
			return BaseResult.success(dictList);
		} catch (Exception e) {
			logger.error("error:",e);
			return BaseResult.exception(e.getMessage());
		}
	}
	
	
	/**
	 * 根据typeName类型查询字典值
	 * @param request
	 * @param typeName
	 * @return
	 */
	@RequestMapping(value = { "/rest/api/searchDictListByTypeName" }, method = RequestMethod.GET)
	public @ResponseBody
	BaseResult searchDictListByTypeName(HttpServletRequest request,String typeName)  {
		try {
			Map<String, Object> params = new HashMap<String, Object>();
			
			params.put("typeName", typeName);
			
    		List<BfDict> dictList = dictService.selectByTypeName(params);
			if(dictList == null || dictList.size()==0)
			{
				return BaseResult.fail("0002", "未查询到值");
			}
			return BaseResult.success(dictList);
		} catch (Exception e) {
			logger.error("error:",e);
			return BaseResult.exception(e.getMessage());
		}
	}
	
}
